<?php
namespace app\admin\controller;

use think\Request;

class Sys extends Base{

    /**
     * 行业类目
     */
    public function index(){
        $list = db('trate')->order('id desc')->paginate('',false,page_param());
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function index_edit(Request $request){
        if($request->isPost()){
            return $this->index_store($request);
        }
        $info = db('trate')->where('id', input('id'))->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function index_del(){
        $params = input();
        $ids = explode(',', $params['id']);
        $info = db('company')->where('trade_id', 'in', $ids)->find();
        if($info){
            return $this->ajaxReturn(1, '删除失败 行业类目已被企业使用');
        }
        $trate_names = db('trate')->where('id', 'in', $ids)->column('name');
        db('trate')->where('id', 'in', $ids)->delete();
        add_sys_log('删除行业类目:'.implode(',', $trate_names));
        return $this->ajaxReturn(0, '成功');
    }

    public function index_store(){
        $params = input();
        if(count($params) == 2){
            //修改状态
            $r = db('trate')->where('id', $params['id'])->update(['status'=>$params['status']]);
            return json_encode(['info'=>'成功', 'status'=>1]);
        }
        $result = $this->validate(
            $params,
            ['name'  => 'require|max:30'],
            ['name.require'=>'行业名称必填', 'name.max'=>'行业名称最多30个字符']
        );
        if(true !== $result){
            return $this->ajaxReturn(1, $result);
        }
        if($params['id']){
            db('trate')->where('id', $params['id'])->update($params);
            add_sys_log('修改行业类目'.$params['name']);
        }else{
            db('trate')->insert($params);
            add_sys_log('添加行业类目'.$params['name']);
        }
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/index'));
    }

    public function index_explode(){
        require EXTEND_PATH.'PHPExcel/PHPExcel.php';
        $excel=new \PHPExcel();
        $letter = array('A');
        $tableheader = array('行业类目名称');
        for($i = 0;$i < count($tableheader);$i++) {
            $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFont()->setBold(true);
            $excel->getActiveSheet()->getColumnDimension($letter[$i])->setWidth(20);
            $excel->getActiveSheet()->getStyle("$letter[$i]1")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFont()->setSize(14);
            $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }
        $data = db('trate')->field('name')->order('id desc')->select();
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
                $excel->getActiveSheet()->getStyle("$letter[$j]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $excel->getActiveSheet()->getStyle("$letter[$j]$i")->getFont()->setSize(14);
                $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
                $j++;
            }
        }
        $write = new \PHPExcel_Writer_Excel5($excel);
        $file_name = '行业类目'.date('Y-m-d',time()).'.xls';
        ob_end_clean();
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$file_name);
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
    }

    /**
     * 经营类目
     */
    public function manage_type(){
        $list = db('manage')->order('id desc')->paginate('',false,page_param());
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function manage_type_edit(Request $request){
        if($request->isPost()){
            return $this->manage_type_store($request);
        }
        $info = db('manage')->where('id', input('id'))->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function manage_type_del(){
        $params = input();
        $ids = explode(',', $params['id']);
        $info = db('company')->where('manage_id', 'in', $ids)->find();
        if($info){
            return $this->ajaxReturn(1, '删除失败 经营类目已被企业使用');
        }
        $manage_names = db('manage')->where('id', 'in', $ids)->column('name');
        db('manage')->where('id', 'in', $ids)->delete();
        add_sys_log('删除经营类目:'.implode(',', $manage_names));
        return $this->ajaxReturn(0, '成功');
    }

    public function manage_type_store(){
        $params = input();
        if(count($params) == 2){
            //修改状态
            $r = db('manage')->where('id', $params['id'])->update(['status'=>$params['status']]);
            return json_encode(['info'=>'成功', 'status'=>1]);
        }
        $result = $this->validate(
            $params,
            ['name'  => 'require|max:30'],
            ['name.require'=>'经营名称必填', 'name.max'=>'经营名称最多30个字符']
        );
        if(true !== $result){
            return $this->ajaxReturn(1, $result);
        }
        if($params['id']){
            db('manage')->where('id', $params['id'])->update($params);
            add_sys_log('修改经营类目:'.$params['name']);
        }else{
            db('manage')->insert($params);
            add_sys_log('添加经营类目:'.$params['name']);
        }
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/manage_type'));
    }

    public function manage_type_explode(){
        require EXTEND_PATH.'PHPExcel/PHPExcel.php';
        $excel=new \PHPExcel();
        $letter = array('A');
        $tableheader = array('经营类目名称');
        for($i = 0;$i < count($tableheader);$i++) {
            $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFont()->setBold(true);
            $excel->getActiveSheet()->getColumnDimension($letter[$i])->setWidth(20);
            $excel->getActiveSheet()->getStyle("$letter[$i]1")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFont()->setSize(14);
            $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }
        $data = db('manage')->field('name')->order('id desc')->select();
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
                $excel->getActiveSheet()->getStyle("$letter[$j]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $excel->getActiveSheet()->getStyle("$letter[$j]$i")->getFont()->setSize(14);
                $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
                $j++;
            }
        }
        $write = new \PHPExcel_Writer_Excel5($excel);
        $file_name = '经营类目'.date('Y-m-d',time()).'.xls';
        ob_end_clean();
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$file_name);
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
    }
    public function log(){
        $s_time = I('s_time','');
        $e_time = I('e_time','');
        $search = I('search','');
        $where= [];
        if(!empty($search)){
            $where['b.account'] = $search;
        }
        if(!empty($s_time)){
            $where['a.create_time']= ['egt',strtotime($s_time)];
        }
        if(!empty($e_time)){
            $where['a.create_time']= ['elt',strtotime($e_time)];
        }
        if(!empty($s_time)&&!empty($e_time)){
            $where['a.create_time'] = ['between',[strtotime($s_time),strtotime($e_time)]];
        }
        $list = M('sys_log a')
            ->join('admin_users b','a.admin_id = b.id','left')
            ->field('a.*,b.name admin_name,b.account')
            ->where($where)
            ->order('a.create_time desc')->paginate('',false,page_param());
        $this->assign('list', $list);
        return $this->fetch();
    }
}